app.controller('AgencyPassengerCtrl', function ($scope, ajax, toaster, $rootScope, authorities) {

	$scope.agencyList = [];
	$scope.queryMap = {};
	$scope.timeRange = {};

	$scope.reload = $scope.page.loader(
		'agency/{userId}/{pageIndex}/{pageSize}/pricipals'.format({ userId: $rootScope.loginUser.id }));
	$scope.$on('pageIndex', function (event, p) { $scope.reload(p, $scope.queryMap); });
	$scope.reload(1);

	$scope.checkQueryMap = function () {
		if ($scope.agencyList.selected != undefined) {
			$scope.queryMap.agencyId = $scope.agencyList.selected.id;
		} else if ($scope.agencyList.selected == undefined) {
			delete $scope.queryMap.agencyId;
		}
		if ($scope.timeRange.startTime && $scope.timeRange.endTime) {
			var starTime = $scope.timeRange.startTime.pattern("yyyy-MM-dd HH:mm:ss");
			var endTime = $scope.timeRange.endTime.pattern("yyyy-MM-dd HH:mm:ss");
			$scope.queryMap.agencyTime = starTime + "@#" + endTime;
		} else {
			delete $scope.queryMap.agencyTime;
		}
	};

	$scope.query = function () {
		$scope.checkQueryMap();
		$scope.reload(1, $scope.queryMap);
	};

	//导出Excel
	$scope.export = function () {
		$scope.checkQueryMap();
		ajax.call(new Req.POST({
			fields: $scope.queryMap,
			resource: 'agency/{userId}/export/pricipals'.format({ userId: $rootScope.loginUser.id })
		})).then(function (result) {
			if (result.code == 200) {
				window.open(result.result);
				toaster.pop('success', '成功', '导出信息成功。');
			} else {
				if (result.code == 423) toaster.pop('warning', '提醒', result.result);
				else toaster.pop('error', '错误', '导出信息失败！！');
			}
		}, function () { toaster.pop('error', '错误', '导出信息失败！'); });
	};

	if (authorities.hasAuthority('BOOKING')) {
		ajax.get('agency/list', function (result) {
			if (result && result.code == 200) $scope.agencyList = result.result;
		}, function () { });
	}
});
